package com.daffodil.storage.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.daffodil.storage.properties.QiniuProperties;
import com.daffodil.storage.properties.StorageProperties;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration.ResumableUploadAPIVersion;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * 
 * @author yweijian
 * @date 2022年12月12日
 * @version 2.0.0
 * @description
 */
@Component
@Configuration
@ConditionalOnProperty(name = StorageProperties.NAME, havingValue = StorageProperties.QINIU)
public class QiniuClientConfig {

    @Bean
    @ConditionalOnMissingBean
    public com.qiniu.storage.Configuration configuration(){
        Region region = Region.autoRegion();
        com.qiniu.storage.Configuration configuration = new com.qiniu.storage.Configuration(region);
        configuration.resumableUploadAPIVersion = ResumableUploadAPIVersion.V2;
        return configuration;
    }
    
    @Bean
    @ConditionalOnMissingBean
    public Auth auth(StorageProperties storageProperties) {
        QiniuProperties properties = storageProperties.getQiniu();
        Auth auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
        return auth;
    }
    
    @Bean
    @ConditionalOnMissingBean
    public UploadManager uploadManager(com.qiniu.storage.Configuration configuration) {
        UploadManager uploadManager = new UploadManager(configuration);
        return uploadManager;
    }
    
    @Bean
    @ConditionalOnMissingBean
    public BucketManager bucketManager(Auth auth, com.qiniu.storage.Configuration configuration) {
        BucketManager bucketManager = new BucketManager(auth, configuration);
        return bucketManager;
    }
    
}
